from manim import *

class TriangleAngleSumWithExtendedLine(Scene):
    def construct(self):
        # 创建一个较小的三角形，确保在屏幕内
        A = np.array([-1, 1, 0])
        B = np.array([2, 1, 0])
        C = np.array([1, 2, 0])
        triangle = Polygon(A, B, C, color=WHITE).move_to(ORIGIN)  # 将三角形居中
        
        # 更新点的位置，使得三角形居中
        A = triangle.get_vertices()[0]
        B = triangle.get_vertices()[1]
        C = triangle.get_vertices()[2]
        
        # 延长线AC至D，使得A, C, D三点在一条直线上
        D = C + (C - A) / 2  # 缩短延长线的长度，确保在屏幕内
        extended_line_AC = Line(A, D, color=WHITE)
        
        # 创建平行线，经过点C，命名为EF
        E = C + LEFT * 3
        F = C + RIGHT * 3
        parallel_line_EF = Line(E, F, color=WHITE)
        
        # 标记三角形的顶点和延长线的顶点
        label_A = MathTex("A").next_to(A, LEFT)
        label_B = MathTex("B").next_to(B, RIGHT)
        label_C = MathTex("C").next_to(C, UP)
        label_D = MathTex("D").next_to(D, RIGHT)
        label_E = MathTex("E").next_to(E, LEFT)
        label_F = MathTex("F").next_to(F, RIGHT)
        
        # 创建标签
        sum_label = MathTex(r"\angle A + \angle B + \angle C = \angle ACD = 180^\circ").to_edge(DOWN)
        corresponding_angle_label = Text("同位角相等").next_to(sum_label, UP)
        
        # 动画序列
        self.play(Create(triangle))
        self.play(Write(label_A), Write(label_B), Write(label_C))
        self.play(Create(extended_line_AC), Write(label_D))
        self.play(Create(parallel_line_EF), Write(label_E), Write(label_F))
        self.wait(1)
        
        # 标记角度
        angle_A = Angle(Line(A, B), Line(A, C), radius=0.6, color=YELLOW)  # 增加角A的弧度
        angle_A_parallel = Angle(Line(C, F), Line(C, D), radius=0.5, color=YELLOW)

        self.play(Create(angle_A))
        self.wait(1)
        self.play(Create(angle_A_parallel))
        self.wait(1)
        self.play(Write(corresponding_angle_label))
        self.wait(1)
        
        # 标记其他角度
        angle_B = Angle(Line(B, C), Line(B, A), radius=0.5, color=RED)
        angle_B_alternate = Angle(Line(C, B), Line(C, F), radius=0.5, color=RED)
        angle_C = Angle(Line(C, A), Line(C, B), radius=0.5, color=BLUE)
        
        self.play(Create(angle_B), Create(angle_B_alternate))

        # 同位角相等的文本转换为内错角相等的文本，并保持位置一致
        new_text = Text("内错角相等").move_to(corresponding_angle_label.get_center())
        self.play(Transform(corresponding_angle_label, new_text))

        self.wait(1)
        self.play(Create(angle_C))
        self.wait(1)
        
        # 突出显示平角ACD，与角C重合
        arc_ACD = Arc(
            radius=0.5, 
            start_angle=Line(C, A).get_angle(), 
            angle=PI, 
            color=PURPLE
        ).move_arc_center_to(C)
        self.play(Create(arc_ACD))
        self.wait(1)
        
        self.play(Write(sum_label))
        self.wait(2)

if __name__ == "__main__":
    scene = TriangleAngleSumWithExtendedLine()
    scene.render()